package team.seekers.skill.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import team.seekers.skill.common.utils.SkillUtils;
import team.seekers.skill.entity.dao.Note;
import team.seekers.skill.entity.query.NoteQuery;
import team.seekers.skill.entity.vo.fore.add.NoteAddVO;
import team.seekers.skill.entity.vo.fore.detail.NoteDetailVO;
import team.seekers.skill.entity.vo.fore.update.NoteUpdateVO;
import team.seekers.skill.result.Result;
import team.seekers.skill.service.NoteService;
import team.seekers.skill.common.utils.ParamCheckUtils;

import java.util.Map;

import static team.seekers.skill.result.ResultCode.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbc
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/api/skill/note")
@Validated
public class NoteController {
    @Autowired
    private NoteService noteService;

    // 根据节id获用户在该节的笔记详情
    @GetMapping("/getBySectionId/{sectionId}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result getNodeVOBySectionId(@PathVariable String sectionId){
        if (ParamCheckUtils.isNotRightId(sectionId)){
            return Result.fail(MAIN_GET_FAIL);
        }
        Note note = noteService.getNoteBySectionId(sectionId);
        if (note == null){
            return Result.fail(GET_FAIL);
        }
        NoteDetailVO noteDetailVO = new NoteDetailVO();
        BeanUtils.copyProperties(note,noteDetailVO);
        return Result.success().data("note",noteDetailVO);
    }

    // 根据id获取用户的笔记详情
    @GetMapping("/get/{noteId}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result get(@PathVariable("noteId") String noteId){
        if (ParamCheckUtils.isNotRightId(noteId)){
            return Result.fail(MAIN_GET_FAIL);
        }
        Note note = noteService.getById(noteId);
        if (note == null){
            return Result.fail(GET_FAIL);
        }
        NoteDetailVO noteDetailVO = new NoteDetailVO();
        BeanUtils.copyProperties(note,noteDetailVO);
        return Result.success().data("note",noteDetailVO);
    }

    //分页获取所有的笔记
    @PostMapping("/pageNoteCondition/{current}/{limit}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result pageNoteCondition(@PathVariable("current") long current,
                                    @PathVariable("limit") long limit,
                                    @RequestBody NoteQuery noteQuery){
        if (ParamCheckUtils.isNotRightId(noteQuery.getSkillId())){
            return Result.fail(MAIN_GET_FAIL);
        }
        if(current<1 || limit<1){
            return Result.fail(MAIN_GET_FAIL);
        }
        Map<String,Object> map = noteService.pageNoteCondition(current, limit, noteQuery);
        if (map == null){
            return Result.fail(GET_FAIL);
        }
        return Result.success(map);
    }

    //删除
    @DeleteMapping("/delete/{noteId}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result deleteNode(@PathVariable String noteId){
        if (ParamCheckUtils.isNotRightId(noteId)){
            return Result.fail(PARAM_NOT_VALID);
        }
        boolean b = noteService.removeById(noteId);
        if (!b){
            return Result.fail(ACTION_FAIL);
        }
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result updateNode(@RequestBody NoteUpdateVO noteVO){
        if (ParamCheckUtils.isNotRightId(noteVO.getId())){
            return Result.fail(ACTION_FAIL);
        }
        Note note = noteService.getById(noteVO.getId());
        if(note == null){
            return Result.fail(PARAM_NOT_VALID);
        }
        BeanUtils.copyProperties(noteVO, note);
        boolean b = noteService.updateById(note);
        if(!b){
            return Result.fail(ACTION_FAIL);
        }
        return Result.success();
    }

    //新增
    @PostMapping("/add")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result addNode(@RequestBody NoteAddVO noteVO){
        Note note = new Note();
        BeanUtils.copyProperties(noteVO, note);
        note.setUserId(SkillUtils.getUserId());
        boolean b = noteService.save(note);
        if(!b){
            return Result.fail(ACTION_FAIL);
        }
        return Result.success();
    }
}

